
import { reqGoodInfo,reqCarGoodeInfo } from "@/api";
import {getUUID} from "@/untils/uuid_token"
const state ={
     goodInfo:{},
     //游客的临时身份
     uuid_token:getUUID()
    
};
//mutations :修改state的唯一手段
const mutations ={
    CATEGOODINFO(state,goodInfo){
        state.goodInfo = goodInfo;
    }
};
//actions:处理action,可以书写自己的业务逻辑,也可以处理异步
const actions ={
    //获取商品的信息
    async getGoodInfo({commit},skuId){
        let result = await reqGoodInfo(skuId);
        console.log(result)
        if(result.code === 200){
              commit('CATEGOODINFO',result.data);
        }
      },
      //添加购物车
      async getCarList({commit},{skuId,skuNum}){
         let result = await reqCarGoodeInfo(skuId,skuNum);
         console.log(result);
         if(result.code == 200){
             return "ok";
         }else{
             return Promise.reject(new Error("faile"))
         }

      }
};
//getters:理解为计算属性,用于简化仓库数据,让组件获取仓库的数据更加方便
const getters ={
    skuInfo(state){
        return state.goodInfo.skuInfo || {};
    },
    categoryView(state){
        return state.goodInfo.categoryView || {};
    },
    spuSaleAttrList(state){
        return state.goodInfo.spuSaleAttrList||[];
    }
  
};

//对外暴露store类的一个实例
export default {
      state,
      mutations,
      actions,
      getters
}